package com.sumainfor.wx.tenant.controller;

import cn.afterturn.easypoi.entity.vo.MapExcelConstants;
import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.sumainfor.common.easypoiExcel.utils.ExcelStyleUtil;
import com.sumainfor.common.easypoiExcel.utils.ExcelUtils;
import com.sumainfor.wx.tenant.entity.ExcleTenant;
import com.sumainfor.wx.tenant.entity.ExcleTenant2;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * 功能描述:
 * Excel导出案例
 * @Auther: zhlu
 * @Date: 2019/6/14 20:32
 * @Description:
 */
@RestController
@RequestMapping("/Excel")
@Log4j2
public class ExcelController {

    /**
     * 功能描述:
     * map形式单sheet导出
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/17 11:11
     */
    @RequestMapping("/export")
    public void downloadByPoiBaseView(ModelMap modelMap, HttpServletRequest request,
                                      HttpServletResponse response) {

        //单Excel
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        ExcelExportEntity excelentity = new ExcelExportEntity("姓名", "name");
        excelentity.setNeedMerge(true);
        entity.add(excelentity);
        entity.add(new ExcelExportEntity("性别", "sex"));
        excelentity = new ExcelExportEntity(null, "students");
        List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
        temp.add(new ExcelExportEntity("姓名", "name"));
        temp.add(new ExcelExportEntity("性别", "sex"));
        excelentity.setList(temp);
        entity.add(excelentity);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map;
        for (int i = 0; i < 10; i++) {
            map = new HashMap<String, Object>();
            map.put("name", "1" + i);
            map.put("sex", "2" + i);

            List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
            tempList.add(map);
            tempList.add(map);
            map.put("students", tempList);

            list.add(map);
        }

        ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
        params.setFreezeCol(2);
        modelMap.put(MapExcelConstants.MAP_LIST, list);
        modelMap.put(MapExcelConstants.ENTITY_LIST, entity);
        modelMap.put(MapExcelConstants.PARAMS, params);
        modelMap.put(MapExcelConstants.FILE_NAME, "EasypoiMapExcelViewTest");
        PoiBaseView.render(modelMap, request, response, MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW);

    }

    /**
     * 功能描述:
     * 多sheet导出案例
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/17 11:10
     */
    @GetMapping("/exportXls")
    public void exportXls(HttpServletRequest request,HttpServletResponse response ){
        //构建第一个sheet内容
        Map<String,Object>map1=new HashMap<>();
        //第一个sheet。sheet名和title名
        ExportParams ep=new ExportParams("表名1","用户sheet1");
        //添加默认的样式
        ep.setStyle(ExcelStyleUtil.class);
        map1.put(NormalExcelConstants.PARAMS,ep);
        map1.put(NormalExcelConstants.CLASS, ExcleTenant.class);
        List<ExcleTenant>list=new ArrayList<ExcleTenant>();
        //构建sheet填充数据
        ExcleTenant excleTenant;
        for (int i=0;i<2;i++){
            excleTenant=new ExcleTenant();
            excleTenant.setId(i);
            excleTenant.setName("张三"+i);
            excleTenant.setScore(i+i);
            list.add(excleTenant);
        }
        map1.put(NormalExcelConstants.DATA_LIST,list);


        Map<String,Object>map2=new HashMap<>();
        ExportParams ep2=new ExportParams("表名2","用户sheet2");
        map2.put(NormalExcelConstants.PARAMS,ep2);
        map2.put(NormalExcelConstants.CLASS, ExcleTenant2.class);
        List<ExcleTenant2>list2=new ArrayList<ExcleTenant2>();
        ExcleTenant2 excleTenant2;
        for (int i=5;i<10;i++){
            excleTenant2=new ExcleTenant2();
            excleTenant2.setUserId(i);
            excleTenant2.setUserName("李四"+i);
            excleTenant2.setAge(i+i);
            list2.add(excleTenant2);
        }
        map2.put(NormalExcelConstants.DATA_LIST,list2);

        List<Map<String,Object>>datelist=new ArrayList<Map<String,Object>>();
        datelist.add(map1);
        datelist.add(map2);

        Workbook workbook= ExcelUtils.exportExcel(datelist,ExcelType.HSSF);
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");
            String filedisplay="product.xls";
            filedisplay= URLEncoder.encode(filedisplay,"UTF-8");
            response.addHeader("Content-Disposition","attachment;filename="+filedisplay);
            OutputStream out=response.getOutputStream();
            workbook.write(out);
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }

    }

}
